/**
 * HappyFarm v2
 *
 * Service implementation
 *
 * @author Cong Liu
 * @time   06/18/2009
 *
 **/

(function(){with(HappyFarm.util){with(HappyFarm.service){

function TkService(){}

TkService.prototype = extend(new BaseService, {
    _start: function() {
        this._last = 0;
        this._running = false;
    },
    _sched: function() {
        if(!getPref('tk', false)) return;
        if(Date.now() - this._last > getPref('tk_timer', 30)*60*1000) {
            this.srvTkAll();
        }
    },
    srvTkAll: function() {
        if(this._running) return;
        var SCM = ServiceControlManager.getInstance();
        var exp = HappyFarm.GlobalData.Exp;
        var friends = [user for each(user in HappyFarm.GlobalData.Friends) if(getPref('tk_type', 'higherexp') != 'higherexp' || user.exp >= exp)];
        var self = this;
        var tkNext = function() {
            self._last = Date.now();
            var f = friends.pop();
            if(f) {
                SCM.callService('UIService', 'UpdateDoing', ['tk '+f.userName+'ing']);
                HappyFarm.api.run(f.userId, 1, tkNext);
            } else {
                SCM.callService('UIService', 'UpdateDoing', ['']);
                self._running = false;
            }
        }
        tkNext();
    }
});

function HarvestService(){}

HarvestService.prototype = extend(new BaseService, {
    _start: function() {
        this._running = false;
        this._min = -1;
        this._last = 0;
    },
    _sched: function() {
        this.srvHarvest();
    },
    matureTime: function(field) {
        return HappyFarm.GlobalData.matureTime(field.cId, field.plantTime, field.state, field.harvestTimes);
    },
    srvHarvest: function() {
        var usertime = HappyFarm.GlobalData.getUserTime(HappyFarm.GlobalData.uId).getTime();
        if(this._last == usertime && HappyFarm.GlobalData.ServerTime < this._min) return;
        if(this._running) return;
        if(!getPref('harvest', false)) return;

        this._last = usertime;
        this._running = true;
        this._min = -1;
        var placeList = [];
        var farmlandStatus = HappyFarm.GlobalData.getUser(HappyFarm.GlobalData.uId).farmlandStatus;
        var sTime = HappyFarm.GlobalData.ServerTime;
        for(var place in farmlandStatus) {
            var matureTime = this.matureTime(farmlandStatus[place]);
            if(matureTime == -1) continue;
            matureTime -= getPref('harvest_timer', 0);
            if(this._min < sTime || this._min > matureTime) this._min = matureTime;
            if(sTime >= matureTime) {
                placeList.push(place);
            }
        }

        if(this._min > 0) log('DEBUG', 'future harvest time: '+ new Date(this._min*1000).toLocaleString() +', current server time: '+new Date(sTime*1000).toLocaleString());

        var self = this;
        var SCM = ServiceControlManager.getInstance();
        var uId = HappyFarm.GlobalData.uId;
        var harvestNext = function() {
            if(placeList.length) {
                SCM.callService('UIService', 'UpdateDoing', ['Harvesting['+place+']']);
                var place = placeList.pop();
                HappyFarm.api.harvest(uId, place, harvestNext);
            } else {
                SCM.callService('UIService', 'UpdateDoing', ['']);
                self._running = false;
            }
        };
        harvestNext();
    }
});

function UIService(){}

UIService.prototype = extend(new BaseService, {
    _start: function() {
        this._doing = '';
        this.srvRefresh();
    },
    srvUpdateDoing: function(doing) {
        this._doing = doing;
        this.srvRefresh();
    },
    srvRefresh: function() {
        HappyFarm.ui.info('RANK: '+HappyFarm.GlobalData.Rank+(this._doing.length?' **'+this._doing+'**':''));
    }
});


HappyFarm.service.impl = {
    TkService: TkService,
    HarvestService: HarvestService,
    UIService: UIService,
//    PlantService,
//    CultivateService,
//    HarvestService,
//    TKService
};

}}})();
